﻿using famails.bus;
using famails.dto;
using FAMailUtils.Data.DAO;
using FAMailUtils.Data.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails
{
    public partial class Master : System.Web.UI.MasterPage
    {
        dto.UserLoginDTO userLogin;
        private const string PARAMETER_NAME = "?enc=";

        AccountDAO utils = new AccountDAO();
        protected void Page_Load(object sender, EventArgs e)
        {

            userLogin = getUserLogin();
            FAMailUtils.Data.DTO.UserLoginDTO user = (FAMailUtils.Data.DTO.UserLoginDTO)Session["UserLogin"];
            //checkRole();
            if (!IsPostBack)
            {

                string page = Request.Url.Segments[Request.Url.Segments.Length - 1].ToLower();
                DataTable group = new FunctionBUS().GetParentByPage(page);
                if (group != null && group.Rows.Count > 0)
                {
                    Session["groupId"] = group.Rows[0]["functionid"];
                    lblGroupMenu.Text = group.Rows[0]["functionname"] + "";
                }

                lblAccountEmail.Text = Session["EmailLogin"] + "";
                lblAccountName.Text = user.UserType == UserType.CustomerAccount ? user.Client.ClientName : user.Account.AccountName;


                string avatar = user.Client.CompanyLogo;
                if (string.IsNullOrWhiteSpace(avatar)) avatar = ResolveClientUrl("http://placehold.it/250x80");
                master_avatar2.Attributes["src"] = master_avatar.Attributes["src"] =
                  logo.Attributes["src"] = logo2.Attributes["src"] = avatar;
                
                string url = "http://mailbox.famails.com/autodirection.aspx";
                //url = "http://localhost:55411/autodirection.aspx";
                string EncryptedString = FaMailUtils.QueryStringModule.Encrypt(userLogin.Username);
                hplMailBox.NavigateUrl = url + PARAMETER_NAME + EncryptedString;
                LoadMainMenu();
            }
        }
        protected void lbtMenu_Click(object sender, EventArgs e)
        {
            LinkButton link = (LinkButton)sender;
            Session["linkId"] = link.Attributes["roleId"];
            Response.Redirect(link.CommandArgument);

        }
        protected void hplSendMail_Click(object sender, EventArgs e)
        {
            LinkButton link = (LinkButton)sender;
            Session["groupId"] = link.Attributes["roleId"];
            Session["linkId"] = link.Attributes["parentId"];
            Response.Redirect(link.CommandArgument);
        }
        void LoadMainMenu()
        {
            if (Session["groupId"] + "" != "")
            {
                bus.RoleListBUS user = new bus.RoleListBUS();
                DataTable menu = user.GetSubMenuByParentId(userLogin.UserId, 1, Session["groupId"]);
                rptMenu.DataSource = menu;
                rptMenu.DataBind();
            }
        }
        private void checkRole()
        {
            RoleListBUS role = new RoleListBUS();
            DataTable menu = role.GetSubMenuByDepartmentId(userLogin.UserType);
            bool result = false;
            foreach (DataRow row in menu.Rows)
            {
                string link = "/" + row["rolelink"] + "";
                result = Request.Url.PathAndQuery.ToLower().IndexOf(link.ToLower()) >= 0;
                if (link != "/" && result)
                {
                    //lblGoupName.Text = role.GetByRoleId(Convert.ToInt32(row["parentid"] + "")).Rows[0]["roleName"].ToString().Replace(" ( Chức năng chính )", "");
                    Session["groupId"] = row["parentId"];
                    Session["linkId"] = row["roleId"];
                    break;
                }
            }
            if (result || userLogin.Username.ToLower() == "administrator") return;
            if (!result && userLogin.Username.ToLower() != "administrator"
                && (Request.Url.PathAndQuery.ToLower().IndexOf("clientdetail.aspx") >= 0
                || Request.Url.PathAndQuery.ToLower().IndexOf("change-pass.aspx") >= 0
                || Request.Url.PathAndQuery.ToLower().IndexOf("forgotpassword.aspx") >= 0
                || Request.Url.PathAndQuery.ToLower().IndexOf("packagechange.aspx") >= 0)
                ) return;
            else Response.Redirect("~/login.aspx?rUrl=" + Request.Url.PathAndQuery);
        }
        private dto.UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                userLogin = (dto.UserLoginDTO)Session["us-login"];
                if (userLogin.UserType != (int)common.Common.DepartmentId.Admin)
                {
                    DataTable client = new ClientBUS().GetByClientID(userLogin.ClientID);
                    if (Convert.ToDateTime(client.Rows[0]["expiredate"]) < DateTime.Now.Date)
                    {
                        Response.Redirect("ClientDetail.aspx");
                    }
                }
                return userLogin;
            }
            else Response.Redirect("~/login.aspx?rUrl=" + Request.Url.PathAndQuery);
            return null;
        }
    }
}